import { customerDataApi } from "@/utils/api";
import { time } from "console";
import moment from "moment";
interface IndexstateType {
  customerList: any;
  customerListOrigin: any;
  currentPage: number; //当前页数
  pageSize: number; //每页显示条目个数
  pageTotal: number; //数据总数
}
export const customerModule = {
  namespaced: true,
  state: {
    //数据
    customerListOrigin: [],
    customerList: [],
    currentPage: 1,
    pageSize: 10,
    pageTotal: 0,
  },
  mutations: {
    //查询
    checkcustomer(state: any, obj: any) {
      const time1 = new Date(obj.time.value[0]).getTime();
      const time2 = new Date(obj.time.value[1]).getTime();
      state.customerList = state.customerListOrigin;
      if (obj.area) {
        state.customerList = state.customerList.filter((v: any) => {
          return v.area === obj.area;
        });
        // console.log(state.customerListOrigin);
      }
      if (time1) {
        state.customerList = state.customerList.filter((v: any) => {
          const time3 = new Date(v.time).getTime();
          return time3 >= time1 && time3 <= time2;
        });
      }

      state.pageTotal = state.customerListOrigin.length;
      state.customerList = state.customerList.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },

    //数据请求
    getCustomerState(state: IndexstateType, payload: any) {
      state.customerListOrigin = payload;

      state.pageTotal = state.customerListOrigin.length;
      state.currentPage = 1;
      state.customerList = state.customerListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },

    //分页
    pageCurrentChange(state: IndexstateType, page: any) {
      state.currentPage = page;

      state.customerList = state.customerListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //给出当前数据
    pageSizeChange(state: IndexstateType, page: any) {
      state.pageSize = page;

      state.customerList = state.customerListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
  },
  actions: {
    //数据请求
    async getcustomerAsync({ commit }: any) {
      const res = await customerDataApi();
      commit("getCustomerState", res.result);
    },
  },
};

export default customerModule;
